from requests_html import HTMLSession
import requests
import time
session = HTMLSession()

# 下载图片
def DownImg( baseSavePath, url):
    response = session.get(url)
    imgpath = response.html.find('img#wallpaper')[0].attrs['src']
    # 获取图片宽度、高度
    imgWidth = response.html.find('img#wallpaper')[
        0].attrs['data-wallpaper-width']
    imgHeght = response.html.find('img#wallpaper')[
        0].attrs['data-wallpaper-height']
    # 过滤小于1080P的图片
    if(int(imgHeght) < 1080 | int(imgWidth) < 1920):
        print("下载失败:照片小于1080p(横屏),不下载")
        return

    # 获取图片
    img_respose = requests.get("https:"+imgpath)
    path = int(round(time.time() * 1000))
    savePath = ('{0}/image{1}.jpg'.format(baseSavePath,path))
    with open(savePath, 'wb') as f:
        f.write(img_respose.content)

    f.close()

    time.sleep(0.1)
    print("下载成功：网络地址："+imgpath+"\n\t保存地址："+savePath)
